/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { BaseMatcher } from '../BaseMatcher.js';
/**
 * Is the value a number equal to a value within some range of
 * acceptable error?
 */
export class IsCloseTo extends BaseMatcher {
    constructor(value, error) {
        super();
        this.delta = error;
        this.value = value;
    }
    matches(item) {
        return this.actualDelta(item) <= 0.0;
    }
    describeMismatch(item, mismatchDescription) {
        mismatchDescription.appendValue(item)
            .appendText(" differed by ")
            .appendValue(this.actualDelta(item))
            .appendText(" more than delta ")
            .appendValue(this.delta);
    }
    describeTo(description) {
        description.appendText("a numeric value within ")
            .appendValue(this.delta)
            .appendText(" of ")
            .appendValue(this.value);
    }
    actualDelta(item) {
        return Math.abs(item - this.value) - this.delta;
    }
    /**
       * Creates a matcher of {@link Double}s that matches when an examined double is equal
       * to the specified <code>operand</code>, within a range of +/- <code>error</code>.
       * For example:
       * <pre>assertThat(1.03, is(closeTo(1.0, 0.03)))</pre>
       *
       * @param operand
       *     the expected value of matching doubles
       * @param error
       *     the delta (+/-) within which matches will be allowed
       * @return The matcher.
       */
    static closeTo(operand, error) {
        return new IsCloseTo(operand, error);
    }
}
